//
//  topicViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/15.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "topicViewController.h"
#import "baseTableview.h"
#import "TopicTableViewCell.h"
#import "baseLabel.h"
#import  "APPTools.h"
#import "TopicModel.h"
#import "TopicDetailViewController.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>
@interface topicViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)baseTableview *TopicTableView;
@property (nonatomic,retain)baseLabel *labelTitle;//题目
@property (nonatomic,retain)NSMutableArray *arrClub;
@property (nonatomic,assign)NSInteger page;//用来保存请求新数据的动态值


@end

@implementation topicViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self creatTableView];
    [self createHeader];
    [self refresh];
    
    self.arrClub = [NSMutableArray array];

    // Do any additional setup after loading the view.
}

#pragma mark - ------------  数据解析 ---------------
-(void)handleData{
    
    NSString *str = @"http://203.80.144.213/napi/topic/list/by_tags/?locale=zh_CN&tags=精选&limit=25&screen_height=568&screen_width=320&start=0&platform_version=9.3.1&app_code=gandalf&device_platform=iPhone5%2C2&platform_name=iPhone%20OS&__domain=www.duitang.com&app_version=6.0.6%20rv%3A156728&device_name=iPhone%205";
    //有中文 转UTf8码
    NSString *urlStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString *url = [urlStr stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", (long)_page]];
    
   [APPTools GETWithURL:url par:nil success:^(id responseObject) {
                NSArray *arr = responseObject[@"data"][@"object_list"];
         NSArray *arr1 = [TopicModel modelHandleWithArrar:arr];
        [self.arrClub addObjectsFromArray:arr1];
       
      
        [self.TopicTableView reloadData];
   } filed:^(NSError *error) {
   }];

}

#pragma mark - ------------ 刷新 加载 ---------------

- (void)refresh
{
    //刷新
    self.TopicTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self handleData];
        [_TopicTableView.mj_header endRefreshing];
    }];
    
    //加载
    self.TopicTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page += 1;
        [self handleData];
        [_TopicTableView.mj_footer endRefreshing];
    }];
    
    
}



#pragma mark - ------------ 热门话题 ---------------
-(void)createHeader{

    self.labelTitle = [[baseLabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.labelTitle.text = @"热门话题";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.font = [UIFont systemFontOfSize:20];
    self.labelTitle.textColor = [UIColor grayColor];
    self.TopicTableView.tableHeaderView = _labelTitle;
    self.TopicTableView.tableHeaderView.dk_backgroundColorPicker = DKColorPickerWithKey(HIGHLIGHTED);
    [_labelTitle release];
    
}

#pragma mark - ------------ 创建tableview ---------------

-(void)creatTableView{
    
    self.TopicTableView = [[baseTableview alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.TopicTableView];
    self.TopicTableView.delegate = self;
    self.TopicTableView.dataSource = self;
    [self.TopicTableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"pool"];
    [_TopicTableView release];
    
}

#pragma mark - ------------ 返回行数 ---------------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _arrClub.count;
   

}


#pragma mark - ------------ cell赋值 ---------------

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    if (_arrClub.count != 0) {
        TopicModel *mo = _arrClub[indexPath.row];
        cell.model = mo;
    }
    
    
    return cell;

}

#pragma mark - ------------ 返回高度 ---------------



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopicModel *model = _arrClub [indexPath.row];
    CGFloat h = [TopicTableViewCell heightWithText:model.content];
    if (model.path != nil) {
        return h+190;
    }else{
    
        return h+60;
    }

}

#pragma mark - ------------ 点击方法 ---------------

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    TopicDetailViewController *detail = [[TopicDetailViewController alloc]init];
   TopicModel *m1 = _arrClub[indexPath.item];
    detail.modelDetail = m1;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];


}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
